package com.example.servicetest

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.AssetManager
import android.graphics.*
import android.text.TextPaint
import android.util.Log
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.content.ContextCompat
import java.io.*


@SuppressLint("StaticFieldLeak")
object MyTextUtils {

    val mColors1:IntArray by lazy{
        intArrayOf(
            ContextCompat.getColor(context,R.color.colorFB8E94),
            ContextCompat.getColor(context,R.color.color0461A7),
            ContextCompat.getColor(context,R.color.color389DAF),
            ContextCompat.getColor(context,R.color.colorFFB502),
            ContextCompat.getColor(context,R.color.colorCE9489),
            ContextCompat.getColor(context,R.color.colorB51E6C),
        )
    }

    val mColors2:IntArray by lazy{
        intArrayOf(
            ContextCompat.getColor(context,R.color.color0361AB),
            ContextCompat.getColor(context,R.color.colorCB247C),
            ContextCompat.getColor(context,R.color.colorEA1D46),
            ContextCompat.getColor(context,R.color.colorEE6C3A),
            ContextCompat.getColor(context,R.color.colorF5DC65),
            ContextCompat.getColor(context,R.color.color2E939B),
        )
    }

    val mColors3:IntArray by lazy{
        intArrayOf(
            ContextCompat.getColor(context,R.color.colorFDEE4B),
            ContextCompat.getColor(context,R.color.colorCB247C),
            ContextCompat.getColor(context,R.color.colorEA1D46),
        )
    }

    val mColors4:IntArray by lazy{
        intArrayOf(
            ContextCompat.getColor(context,R.color.colorFB8E94),
            ContextCompat.getColor(context,R.color.color0361AB),
            ContextCompat.getColor(context,R.color.color3B9EB1),
            ContextCompat.getColor(context,R.color.colorFFB504),
            ContextCompat.getColor(context,R.color.colorCE9489),
            ContextCompat.getColor(context,R.color.colorB31F69),
        )
    }

    val mColors5:IntArray by lazy{
        intArrayOf(
            ContextCompat.getColor(context,R.color.colorCB247C),
            ContextCompat.getColor(context,R.color.colorEA1D46),
            ContextCompat.getColor(context,R.color.colorEE6C3A),
            ContextCompat.getColor(context,R.color.colorF5DC65),
            ContextCompat.getColor(context,R.color.color2E939B),
        )
    }

    val mColors6:IntArray by lazy{
        intArrayOf(
            ContextCompat.getColor(context,R.color.colorFDEE4B),
            ContextCompat.getColor(context,R.color.colorF9096E),
            ContextCompat.getColor(context,R.color.color2B2AF0),
        )
    }

    val mLinearGradientColors:MutableList<IntArray> by lazy {
        mutableListOf(
            mColors1,mColors2,mColors3,mColors4,mColors5,mColors6
        )
    }

    @JvmStatic
    lateinit var context: Context

    fun init(context: Context){
        this.context = context
    }

    fun getBitmapFromText(
        text: Char,
//        width: Int = 44,
        textSize: Float = 200f,
        textColor: Int = Color.parseColor("#D520F4"),
        font: String = "font/FZXIANGSU16.TTF",
        isBold: Boolean = true,
        isScan: Boolean = true,
    ): Bitmap {
        val paint = TextPaint()

        paint.textSize = textSize
        paint.color = textColor
        paint.style = Paint.Style.FILL
        paint.isFakeBoldText = isBold
//        paint.isAntiAlias = true
//        paint.flags = Paint.ANTI_ALIAS_FLAG;
//        paint.isFilterBitmap = true
        val typeface:Typeface?
        try {
            val asset: AssetManager = context.assets
            typeface = Typeface.createFromAsset(asset,font)
            paint.typeface = typeface
        }catch (e:Exception){
            Log.e("TAG","文件错误:${e.message}")
        }
        val rect = Rect()
        paint.getTextBounds(text.toString(), 0, 1, rect)

        val fm: Paint.FontMetricsInt = paint.fontMetricsInt

        val width = paint.measureText(text.toString()).toInt()
        val height: Int = fm.descent - fm.ascent

        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)

        canvas.drawText(text.toString(), 0f, (fm.leading - fm.ascent).toFloat(), paint)

        if(isScan){
            val matrix = Matrix()
            val ratio: Float = 16/bitmap.height.toFloat()
            matrix.postScale(ratio, ratio)
            val resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
            bitmap.recycle()
            return resizeBmp
        }
        return bitmap
    }

    @JvmStatic
    fun getBgBitmap(
        colors: IntArray,
        isTransition: Boolean = true,
        width: Int = 44,
        height: Int = 16,
        @LinearLayoutCompat.OrientationMode orientation:Int = LinearLayoutCompat.VERTICAL,
    ): Bitmap {
        val paint = Paint()
        paint.style = Paint.Style.FILL
        paint.shader = LinearGradient(
            0f,
            0f,
            if(orientation == LinearLayoutCompat.VERTICAL) 0f else width.toFloat(),
            if(orientation == LinearLayoutCompat.VERTICAL) height.toFloat() else 0f,
            if(isTransition) getColorFromRes(colors) else colors,
            null,
            Shader.TileMode.REPEAT
        )
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        canvas.drawRect(Rect(0,0,width,height),paint)
        paint.shader = null
        return bitmap
    }

    @JvmStatic
    fun getColorFromRes(
        colors: IntArray,
    ): IntArray {
        val colorsForm = IntArray(colors.size)
        for(i in colors.indices){
            colorsForm[i] = colors[i]
        }
        return colorsForm
    }

    fun getColorArray(bitmap: Bitmap): MutableList<MutableList<Int>> {
        val color2DArray = mutableListOf<MutableList<Int>>()
        for (columnIndex in 0 until bitmap.height) {
            val rowColorArray = mutableListOf<Int>()
            for (rowIndex in 0 until bitmap.width) {
                rowColorArray.add(bitmap.getPixel(rowIndex, columnIndex))
            }
            color2DArray.add(rowColorArray)
        }
        return color2DArray
    }



}